package com.treetao.union.web.controller.mall;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.treetao.union.db.model.MallPAttr;
import com.treetao.union.db.model.MallPAttrItem;
import com.treetao.union.db.model.MallPService;
import com.treetao.union.db.model.MallPSkuService;
import com.treetao.union.db.service.MallPServiceService;
import com.treetao.union.db.service.MallPSkuServiceService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.treetao.union.db.model.vo.LayerData;
import com.treetao.union.db.model.vo.RestResponse;
import com.treetao.union.web.constant.Constants;
import com.treetao.union.web.controller.BaseController;
import com.treetao.union.web.pojo.XmSelectPojo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.WebUtils;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import java.util.Map;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 服务保障  前端控制器
 * </p>
 *
 * @author Dev.M
 * @since 2020-04-11
 */
@Controller
@RequestMapping(Constants.AMIND_PREFIX + "/mall-pservice")
public class MallPServiceController extends BaseController{

    @Autowired
    private MallPServiceService mallPServiceService;
    @Autowired
    private MallPSkuServiceService mallPSkuServiceService;

    @GetMapping("list")
    public String list(){
        return "/admin/mall-pservice/list";
    }

    @PostMapping("list")
    @ResponseBody
    public LayerData<MallPService> list(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                      @RequestParam(value = "limit",defaultValue = "10")Integer limit,
                                      ServletRequest request){
        Map map = WebUtils.getParametersStartingWith(request, "s_");
        LayerData<MallPService> layerData = new LayerData<>();
        QueryWrapper<MallPService> wrapper = new QueryWrapper<MallPService>();
        if(!map.isEmpty()){
        	String delFlag = (String) map.get("delFlag");
            if(StringUtils.isNotBlank(delFlag)) {
                wrapper.eq("del_flag",Boolean.valueOf(delFlag));
            }else{
                map.remove("del_flag");
            }
        }
        IPage<MallPService> pageData = mallPServiceService.page(new Page<>(page,limit),wrapper);
        layerData.setData(pageData.getRecords());
        layerData.setCount(pageData.getTotal());
        return layerData;
    }
    
    @GetMapping("xmSelectData")
	@ResponseBody
	public List<XmSelectPojo> xmSelectData(HttpServletRequest req) {
    	Long skuId = null;
    	if(req.getParameter("skuId")!=null) {
    		skuId = Long.valueOf(req.getParameter("skuId"));
    	}
    	List<MallPSkuService> services = mallPSkuServiceService.getSkuServices(skuId);
    	
		List<XmSelectPojo> result = new ArrayList<XmSelectPojo>();
		List<MallPService> attrs = mallPServiceService.list(getQueryWrapperNoDel());
		if(attrs!=null) {
			attrs.forEach(attr -> {
				XmSelectPojo xsp = new XmSelectPojo();
				xsp.setName(attr.getName());
				xsp.setValue(attr.getId());
				xsp.setDisabled(attr.getDelFlag());
				
				if(services!=null) {
					services.forEach(serviceItem -> {
						if(serviceItem.getRefServiceId().longValue() == attr.getId().longValue()) {
							xsp.setSelected(true);
						}
					});
				}
				
				result.add(xsp);
			});
		}
		return result;
	}

    @GetMapping("add")
    public String add(){
        return "/admin/mall-pservice/add";
    }

    @PostMapping("add")
    @ResponseBody
    public RestResponse add(MallPService mallPService){
        mallPServiceService.save(mallPService);
        return RestResponse.success();
    }
    
    @GetMapping("detail")
    public String detail(Long id,Model model){
       MallPService mallPService = mallPServiceService.getById(id);
        model.addAttribute("mallPService",mallPService);
        return "/admin/mall-pservice/detail";
    }

    @GetMapping("edit")
    public String edit(Long id,Model model){
        MallPService mallPService = mallPServiceService.getById(id);
        model.addAttribute("mallPService",mallPService);
        return "/admin/mall-pservice/edit";
    }

    @PostMapping("edit")
    @ResponseBody
    public RestResponse edit(MallPService mallPService){
        if(null == mallPService.getId() || 0 == mallPService.getId()){
            return RestResponse.failure("ID不能为空");
        }
        mallPServiceService.updateById(mallPService);
        return RestResponse.success();
    }
    
    @PostMapping("batchDel")
    @ResponseBody
    public RestResponse batchDel(@RequestParam(value = "ids[]",required = false)List<Long> ids){
        if(null == ids || ids.size()==0){
            return RestResponse.failure("请选择一项");
        }
        return RestResponse.success().setDataOnlyResult( mallPServiceService.removeByIds(ids));
    }

    @PostMapping("delete")
    @ResponseBody
    public RestResponse delete(@RequestParam(value = "id",required = false)Long id){
        if(null == id || 0 == id){
            return RestResponse.failure("ID不能为空");
        }
        return RestResponse.success().setDataOnlyResult(mallPServiceService.removeById(id));
    }
    
    @PostMapping("change-status")
    @ResponseBody
    public RestResponse change_status(@RequestParam(value = "id",required = true)Long id,@RequestParam(value = "status",required = false)Boolean status){
       MallPService mallPService = mallPServiceService.getById(id);
        mallPService.setDelFlag(status);
        mallPServiceService.updateById(mallPService);
        return RestResponse.success();
    }

}